import { API_HOST } from '../assets/constant/host.js'
import { getToken, clearStorage } from './storage.js'
import { showWxToast, showWxLoading, hideWxLoading } from './index.js'

export function request(url, method = 'GET', data) {
    return new Promise((resolve, reject) => {
        const tmpToken = getToken();
        if (!data.cancelLoading) {
            showWxLoading('请求中');
        }
        wx.request({
            url: API_HOST + url,
            method,
            data,
            header: {
                token: tmpToken,
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            success: res => {
                if (!data.cancelLoading) {
                    hideWxLoading();
                }
                if (res.statusCode === 200) {
                    if (res.data.success) {
                        resolve(res.data);
                    } else {
                        if (res.data.message === '401') {
                            showWxToast('登录已过期,请重新授权登录!');
                            clearStorage();
                            reject('401');
                        } else {
                            resolve(res.data);
                        }
                    }
                } else {
                    showWxToast('网络异常');
                    reject('网络异常');
                }
            },
            fail: (err) => {
                if (!data.cancelLoading) {
                    hideWxLoading();
                }
                showWxToast('网络异常');
                reject('网络异常');
            },
        });
    })
}

export function requestUpload(url, path) {
    return new Promise((resolve, reject) => {
        const tmpToken = getToken();
        wx.uploadFile({
            url: 'https://www.009bar.com/' + url,
            filePath: path,
            name: 'file',
            header: {
                'token': tmpToken,
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            formData: {
                prefix: 'wechat-complaints'
            },
            success: function (res) {
                resolve(res);
            },
            fail: function (err) {
                reject(err);
            }
        })
    });
}

export function $request(url, method = 'GET', data) {
    return new Promise((resolve, reject) => {
        const tmpToken = getToken();
        if (!data.cancelLoading) {
            showWxLoading('请求中');
        }
        let tmpDomain = 'https://www.009bar.com/';
        if(data.domain) {
          tmpDomain = data.domain;
        }
        wx.request({
            url: tmpDomain + url,
            method,
            data,
            header: {
                token: tmpToken,
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            success: res => {
                if (!data.cancelLoading) {
                    hideWxLoading();
                }
                if (res.statusCode === 200) {
                    if (res.data.success) {
                        resolve(res.data);
                    } else {
                        if (res.data.message === '401') {
                            showWxToast('登录已过期,请重新授权登录!');
                            clearStorage();
                            reject('401');
                        } else {
                            resolve(res.data);
                        }
                    }
                } else {
                    showWxToast('网络异常');
                    reject('网络异常');
                }
            },
            fail: (err) => {
                if (!data.cancelLoading) {
                    hideWxLoading();
                }
                showWxToast('网络异常');
                reject('网络异常');
            },
        });
    })
}